package com.yezi.lampcourt.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yezi.lampcourt.R;
import com.yezi.lampcourt.base.BaseActivity;
import com.yezi.lampcourt.entity.CourtBean;
import com.yezi.lampcourt.entity.ExecuteResultLampBean;
import com.yezi.lampcourt.entity.LampBean;
import com.yezi.lampcourt.entity.ModeBean;
import com.yezi.lampcourt.ui.adapter.ModeAdapter;
import com.yezi.lampcourt.ui.contract.CourtContract;
import com.yezi.lampcourt.ui.presenter.CourtPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by HongMing Ye
 * on 2017/10/9 0009.
 */

public class CourtActivity extends BaseActivity<CourtPresenter> implements CourtContract.CourtView {
    @BindView(R.id.tv_court_name)
    TextView mTvCourtName;
    @BindView(R.id.rv_lamp_mode_list)
    RecyclerView mRvLampModeList;
    @BindView(R.id.lamp_1)
    ImageView mLamp1;
    @BindView(R.id.lamp_2)
    ImageView mLamp2;
    @BindView(R.id.lamp_5)
    ImageView mLamp5;
    @BindView(R.id.lamp_6)
    ImageView mLamp6;
    @BindView(R.id.lamp_3)
    ImageView mLamp3;
    @BindView(R.id.lamp_4)
    ImageView mLamp4;
    @BindView(R.id.lamp_7)
    ImageView mLamp7;
    @BindView(R.id.lamp_8)
    ImageView mLamp8;

    private CourtBean mCourtBean;
    private List<LampBean> mLampBeanList;
    private List<ModeBean> mModeBeanList;
    private List<ImageView> lampList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_court);
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void init() {
        lampList = new ArrayList<>();
        lampList.add(mLamp1);
        lampList.add(mLamp2);
        lampList.add(mLamp3);
        lampList.add(mLamp4);
        lampList.add(mLamp5);
        lampList.add(mLamp6);
        lampList.add(mLamp7);
        lampList.add(mLamp8);

        mCourtBean = getIntent().getParcelableExtra("courtBean");
        mTvCourtName.setText(mCourtBean.getCourtName());

        mPresenter = new CourtPresenter(this, this);
        mPresenter.getLampList(mCourtBean.getCollectid());
        mPresenter.getModeList();

        mRvLampModeList.setLayoutManager(new GridLayoutManager(this, 4));
    }

    @Override
    public void getLampListSuccess(List<LampBean> lampBeanList) {
        mLampBeanList = lampBeanList;
        for (int i = 0; i < lampBeanList.size(); i++) {
            if (i < lampList.size()) {
                if (lampList.get(i) != null) {
                    lampList.get(i).setSelected(true);
                }
            } else {
                if (i < lampList.size()) {
                    lampList.get(i).setSelected(false);
                }
            }
        }
    }

    @Override
    public void getLampListFail(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getLampModeSuccess(List<ModeBean> modeList) {
        mModeBeanList = modeList;
        final ModeAdapter modeAdapter = new ModeAdapter(R.layout.recycler_item_mode, mModeBeanList);
        modeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < mModeBeanList.size(); i++) {
                    if (i != position) {
                        mModeBeanList.get(i).setSelected(false);
                    } else {
                        mModeBeanList.get(i).setSelected(true);
                        mPresenter.executeMode(mCourtBean.getCollectid(), mModeBeanList.get(i).getModeId());
                        showLoading("执行中，请稍后...");
                    }
                }
                modeAdapter.setNewData(mModeBeanList);
            }
        });
        mRvLampModeList.setAdapter(modeAdapter);
    }

    @Override
    public void getLampModeFail(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void executeModeSuccess(List<ExecuteResultLampBean> lampBeanList) {
        hideLoading();
        for (int i = 0; i < lampBeanList.size(); i++) {
            if (lampBeanList.get(i).getNowBrightness() > 0) {
                if (i < lampList.size()) {
                    lampList.get(i).setSelected(true);
                }
            } else {
                if (i < lampList.size()) {
                    lampList.get(i).setSelected(false);
                }
            }
        }
        Toast.makeText(this, "执行完成，请等待机器响应！", Toast.LENGTH_SHORT).show();
    }


    @Override
    public void executeModeFail(String msg) {
        hideLoading();
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
